<?php
class model_categories extends database_mysql
{
	public function mdc_cat_list($id=0,$noid=0)
    {
        $id = parent::mysql_check_string($id);
        $noid = parent::mysql_check_string($noid);
        
        $row = parent::mysql_fetch_all_rows("
            SELECT * FROM
                ". prefix ."categories
                INNER JOIN ". prefix ."categories_lang 
                ON ". prefix ."categories.ID = ". prefix ."categories_lang.CatID
            WHERE
                ". prefix ."categories.PID = '".$id."' AND
                ". prefix ."categories.ID <> '".$noid."' AND
                ". prefix ."categories_lang.LangID = '".LANG."'
            ORDER BY ". prefix ."categories.Order ASC
        ");
        
        parent::mysql_close_connection();
        
 		return $row;
    }

    public function mdc_get_cat_com($define,$Com)
    {
        
        $Com = parent::mysql_check_string($Com);
        $define = parent::mysql_check_string($define);
        
        $row = parent::mysql_fetch_one_rows("
            SELECT * FROM
                ". prefix ."categories
                INNER JOIN ". prefix ."categories_lang 
                ON ". prefix ."categories.ID = ". prefix ."categories_lang.CatID
            WHERE
                ". prefix ."categories.Define = '".trim($define)."' AND
                ". prefix ."categories.Com = '".trim($Com)."' AND
                ". prefix ."categories_lang.LangID = '".LANG."'
        ");
        
        parent::mysql_close_connection();
        
 		return $row;
    }
    public function mdc_get_pcat($pid)
    {
        $pid = parent::mysql_check_string($pid);

        $row = parent::mysql_fetch_one_rows("
            SELECT ". prefix ."categories.ID FROM
                ". prefix ."categories
            WHERE
                ". prefix ."categories.PID = '".trim($pid)."'
        ");
        $id = isset($row['ID'])?$row['ID']:NULL;
        parent::mysql_close_connection();

        return $id;
    }
    public function mdc_get_cat($catid)
    {
        $catid = parent::mysql_check_string($catid);
        
        $row = parent::mysql_fetch_one_rows("
            SELECT * FROM
                ". prefix ."categories
            WHERE
                ". prefix ."categories.ID = '".trim($catid)."'
        ");
        
        parent::mysql_close_connection();
        
 		return $row;
    }
    
    public function mdc_get_seo_key($seokey,$lang_id,$catid=null)
    {
        $str=null;
        $seokey = parent::mysql_check_string($seokey);
        if(!empty($catid))
        {
            $str = " ". prefix ."categories_lang.CatID <> '".trim($catid)."' AND ";
        }
        $row = parent::mysql_fetch_one_rows("
            SELECT count(CatID) as total FROM
                ". prefix ."categories_lang
            WHERE
                ".$str."
                ". prefix ."categories_lang.SeoKey = '".trim($seokey)."' AND
                ". prefix ."categories_lang.LangID = '".$lang_id."'
        ");
        parent::mysql_close_connection();
 		return isset($row['total'])?$row['total']:NULL;
    }
    
    public function mdc_get_cat_lang($catid,$lang_id)
    {
        $catid = parent::mysql_check_string($catid);
        
        $row =   parent::mysql_fetch_one_rows("
            SELECT * FROM
                ". prefix ."categories_lang
            WHERE
                ". prefix ."categories_lang.CatID = '".trim($catid)."' AND
                ". prefix ."categories_lang.LangID = '".$lang_id."'
        ");
        
        parent::mysql_close_connection();
        
 		return $row;
    }
    
    public function mdc_cat_list_box($id=0,$noid=0,$com=NULL)
    {
        $id =       parent::mysql_check_string($id);
        $noid =     parent::mysql_check_string($noid);
        $strcom =   $com!=null?" ". prefix ."categories.Com = '".$com."' AND ":null;
        
        $row = parent::mysql_fetch_all_rows("
            SELECT 
                ". prefix ."categories.ID,
                ". prefix ."categories.Com,
                ". prefix ."categories_lang.CatName
            FROM
                ". prefix ."categories
                INNER JOIN ". prefix ."categories_lang 
                ON ". prefix ."categories.ID = ". prefix ."categories_lang.CatID
            WHERE
                ". prefix ."categories.PID = '".$id."' AND
                ".$strcom."
                ". prefix ."categories.ID <> '".$noid."' AND
                ". prefix ."categories.ID <> 1 AND
                ". prefix ."categories_lang.LangID = '".LANG."'
            ORDER BY ". prefix ."categories.Order ASC
        ");
        
        parent::mysql_close_connection();
        
 		return $row;
    }
    
    public function mdc_cat_com_box()
    {        
        $row = parent::mysql_fetch_all_rows("
            SELECT 
                ". prefix ."categories.ID as ID,
                ". prefix ."categories.Com as com_value  ,
                ". prefix ."categories_lang.CatName as com_text
            FROM
                ". prefix ."categories
                INNER JOIN ". prefix ."categories_lang 
                ON ". prefix ."categories.ID = ". prefix ."categories_lang.CatID
            WHERE
                ". prefix ."categories.Define = 'com' AND
                ". prefix ."categories_lang.LangID = '".LANG."' 
            ORDER BY ". prefix ."categories.Order ASC
        ");
        
        parent::mysql_close_connection();
        
 		return $row;
    }

    public function mdc_set_order($arr_id=0) 
    {
        $rs ='';
        foreach($arr_id as $key => $id)
        {   
            $key = $key+1;
            $key = parent::mysql_check_string($key);
            $id = parent::mysql_check_string($id);
            $select = "UPDATE ". prefix ."categories SET ". prefix ."categories.Order='$key' WHERE `ID` = '$id'";
            $rs = parent::mysql_update_insert($select);
        }
        return $rs;
    }
    
    public function mdc_cat_insert()
    {
      
        $PID =              parent::mysql_check_string($_POST['PID']);
        $Order =            parent::mysql_check_string($_POST['Order']);
        $Com =              parent::mysql_check_string($_POST['Com']);
        $HorizontalMenu =   isset($_POST['HorizontalMenu'])?parent::mysql_check_string($_POST['HorizontalMenu']):0;
        $VerticalMenu =     isset($_POST['VerticalMenu'])?parent::mysql_check_string($_POST['VerticalMenu']):0;
        $Img =              isset($_POST['Img'])?$_POST['Img']:NULL;
        
        $update = "INSERT INTO ".prefix."categories(PID,Img,`Order`,`Define`,App,Com,Act,HorizontalMenu,VerticalMenu) VALUES ('". $PID ."','". $Img ."','". $Order ."','sub','public','". $Com ."',1,'". $HorizontalMenu ."','". $VerticalMenu ."')";
        parent::mysql_update_insert($update);
        $done = mysql_insert_id();
        
        parent::mysql_close_connection();
		return $done;
        
    }

    public function mdc_cat_insert_lang($id,$LangID)
    {
        $done = '';
        $CatName =  parent::mysql_check_string($_POST['CatName'][$LangID]);
        $SeoKey =   parent::mysql_check_string($_POST['SeoKey'][$LangID]);
        $Title =    parent::mysql_check_string($_POST['Title'][$LangID]);
        $Desc =     parent::mysql_check_string($_POST['Desc'][$LangID]);
        $Keyword =  parent::mysql_check_string($_POST['Keyword'][$LangID]);
        $TitleH1 =  parent::mysql_check_string($_POST['TitleH1'][$LangID]);
        $TitleH2 =  parent::mysql_check_string($_POST['TitleH2'][$LangID]);
        $DescF =    parent::mysql_check_string($_POST['DescF'][$LangID]);
        $Status =   parent::mysql_check_string($_POST['Status'][$LangID]);
        
        if(!empty($_POST['CatName'][$LangID]) && !empty($_POST['SeoKey'][$LangID]))
        {
            $update = "INSERT INTO ".prefix."categories_lang(`CatID`,`LangID`,`SeoKey`,`CatName`,`Title`,`Desc`,`Keyword`,`TitleH1`,`TitleH2`,`DescF`,`Status`) VALUES ('".$id."','".$LangID."','".$SeoKey."','".$CatName."','".$Title."','".$Desc."','".$Keyword."','".$TitleH1."','".$TitleH2."','".$DescF."','".$Status."')";
            $done = parent::mysql_update_insert($update);
        }
        parent::mysql_close_connection();
		return $done;
        
    }

    public function mdc_cat_update($id)
    {
        $PID =              parent::mysql_check_string($_POST['PID']);
        $Order =            parent::mysql_check_string($_POST['Order']);
        $Com =              parent::mysql_check_string($_POST['Com']);
        $HorizontalMenu =   isset($_POST['HorizontalMenu'])?parent::mysql_check_string($_POST['HorizontalMenu']):0;
        $VerticalMenu =     isset($_POST['VerticalMenu'])?parent::mysql_check_string($_POST['VerticalMenu']):0;
        $imgname =          isset($_POST['Img'])?$_POST['Img']:NULL;
        $imgname =          parent::mysql_check_string($imgname);
        $Img =              empty($_POST['Img'])?null:" Img='". $imgname ."',";
        $Define = "`Define`='sub',";

        if($id == 2)
        { 
            $PID = 0;
            $Com = 'index';
            $Define = "`Define`='app',";
        }

        $update = "UPDATE ".prefix."categories SET PID='". $PID ."', ". $Img ." `Order`='". $Order ."', ". $Define ." App='public',Com='". $Com ."',Act=1,HorizontalMenu='". $HorizontalMenu ."',VerticalMenu='". $VerticalMenu ."' WHERE `ID` = '".$id."'";
        $done = parent::mysql_update_insert($update);

        parent::mysql_close_connection();
		return $done;
        
    }

    public function mdc_cat_update_lang($id,$LangID)
    {
        $done = '';
        $CatName =  parent::mysql_check_string($_POST['CatName'][$LangID]);
        $SeoKey =   parent::mysql_check_string($_POST['SeoKey'][$LangID]);
        $Title =    parent::mysql_check_string($_POST['Title'][$LangID]);
        $Desc =     parent::mysql_check_string($_POST['Desc'][$LangID]);
        $Keyword =  parent::mysql_check_string($_POST['Keyword'][$LangID]);
        $TitleH1 =  parent::mysql_check_string($_POST['TitleH1'][$LangID]);
        $TitleH2 =  parent::mysql_check_string($_POST['TitleH2'][$LangID]);
        $DescF =    parent::mysql_check_string($_POST['DescF'][$LangID]);
        $Status =   parent::mysql_check_string($_POST['Status'][$LangID]);
        
        if($id == 2)
        { 
            $SeoKey = '';
        }
        
        if(!empty($_POST['CatName'][$LangID]) && !empty($_POST['SeoKey'][$LangID]))
        {
            $aid = $this->mdc_get_cat_lang($id,$LangID);
            $xid = count($aid);
            if($xid>0)
            {
                $update = "UPDATE  ".prefix."categories_lang SET `SeoKey`='".$SeoKey."',`CatName`='".$CatName."',`Title`='".$Title."',`Desc`='".$Desc."',`Keyword`='".$Keyword."',`TitleH1`='".$TitleH1."',`TitleH2`='".$TitleH2."',`DescF`='".$DescF."', `Status`='".$Status."' WHERE `CatID` = '".$id."' AND `LangID` = '".$LangID."' ";
                $done = parent::mysql_update_insert($update);
                
            }else
            {
                $update = "INSERT INTO ".prefix."categories_lang(`CatID`,`LangID`,`SeoKey`,`CatName`,`Title`,`Desc`,`Keyword`,`TitleH1`,`TitleH2`,`DescF`,`Status`) VALUES ('".$id."','".$LangID."','".$SeoKey."','".$CatName."','".$Title."','".$Desc."','".$Keyword."','".$TitleH1."','".$TitleH2."','".$DescF."','".$Status."')";
                $done = parent::mysql_update_insert($update);
            }
        }
        parent::mysql_close_connection();
		return $done;
        
    }

    public function mdc_cat_del_pic($id)
    {
        $id = parent::mysql_check_string($id);
        $update = "UPDATE ".prefix."categories SET Img='' WHERE `ID` = '".$id."'";
        $done = parent::mysql_update_insert($update);
        parent::mysql_close_connection();
        return $done;

    }

    public function mdc_cat_get_pic($id)
    {
        $id = parent::mysql_check_string($id);
        $sql = "SELECT Img FROM ".prefix."categories WHERE `ID` = '".$id."'";
        $row = parent::mysql_fetch_one_rows($sql);
        parent::mysql_close_connection();
        return isset($row['Img'])?$row['Img']:null;
    }

    public function mdc_tool_func($model,$active,$str_id)
    {
        $done = false;

        switch($active)
        {
            case 'show':
            case 'hide':
            case 'del':
                $done = $model->model_tool_set_status(prefix."categories_lang",$active," CatID IN(".trim($str_id).")");
            break;

            case 'delete':
                $str_id = str_replace('2,','',$str_id);
                $arr = explode(',',$str_id);
                if(!empty($arr))
                {
                    foreach($arr as $id)
                    {
                        $img = $this->mdc_cat_get_pic($id);
                        if(!empty($img))
                        unlink(PATH_BASE.'upload/images/categories/'.$img);
                    }
                }
                $done = $model->model_tool_delete(prefix."categories"," ID IN(".trim($str_id).")" );
                $done = $model->model_tool_delete(prefix."categories_lang"," CatID IN(".trim($str_id).")" );

            break;

            case 'order':
                $arr_id = explode(',',$str_id);
                $done = $this->mdc_set_order($arr_id);
            break;
        }

        return $done;
    }
    
    
}
